package com.huluobo.a2208parkdemo

import android.content.Intent
import android.util.Log
import android.view.View
import android.webkit.JavascriptInterface
import com.blankj.utilcode.util.SPUtils
import com.huluobo.a2208parkdemo.databinding.ActivityMainBinding
import com.huluobo.a2208parkdemo.news.NewsActivity
import com.huluobo.a2208parkdemo.notice.NoticeActivity
import com.huluobo.a2208parkdemo.parking.ParkingActivity
import com.huluobo.a2208parkdemo.patrol.PatrolActivity
import com.huluobo.a2208parkdemo.repair.AddRepairActivity
import com.huluobo.a2208parkdemo.repair.RepairActivity
import com.huluobo.a2208parkdemo.sign.SignActivity
import com.huluobo.a2208parkdemo.sign.SignMonthActivity
import com.huluobo.a2208parkdemo.visitor.VisitorActivity
import com.huluobo.base.mvvm.BaseActivity
import com.huluobo.base.net.Const
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class MainActivity : BaseActivity<ActivityMainBinding, MainViewModel>() {
    private lateinit var listener: MainWebViewJavaScriptFun
    override fun initData() {

    }

    override fun initView() {
        binding.mainHeader.headerBack.visibility = View.GONE
        initWebView()
    }

    private fun initWebView() {
        listener = MainWebViewJavaScriptFun()
        val home = SPUtils.getInstance().getString(Const.PARAM_HOME)
        binding.mainX5.loadUrl(home)
        //让当前网页内容可滚动
        binding.mainX5.overScrollMode = View.OVER_SCROLL_ALWAYS
        binding.mainX5.addJavascriptInterface(listener, "androidinfo")

        requestPermissions(
            arrayOf(
                android.Manifest.permission.ACCESS_COARSE_LOCATION,
                android.Manifest.permission.ACCESS_FINE_LOCATION,
                android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
                android.Manifest.permission.READ_EXTERNAL_STORAGE,
            ), 102
        )

    }

    inner class MainWebViewJavaScriptFun() {
        @JavascriptInterface
        fun androidsign(id: String) {
            //打开签到activity
            start(SignActivity::class.java)
        }

        @JavascriptInterface
        fun androidaddculture(id: String) {
            //打开新闻activity
            start(NewsActivity::class.java)
        }

        @JavascriptInterface
        fun androidparking(id: String?) {
            //车位申请
            start(ParkingActivity::class.java)
        }

        @JavascriptInterface
        fun androidapply(id: String?) {
            //我的申请
            Log.i("TagA", "我的申请")
        }

        @JavascriptInterface
        fun androidvisit(id: String?) {
            //来访预约
            start(VisitorActivity::class.java)
        }

        @JavascriptInterface
        fun androidpatrol(id: String?) {
            //巡更管理
            start(PatrolActivity::class.java)
        }

        @JavascriptInterface
        fun androidnews(id: String?) {
            Log.i("TagA", "新闻")
        }

        @JavascriptInterface
        fun androidnoticelist(id: String?) {
            Log.i("TagA", "公告列表")
        }

        @JavascriptInterface
        fun androidrepair(id: String?) {
            //维修管理
            start(RepairActivity::class.java)
        }

        @JavascriptInterface
        fun androidaddrepair(id: String?) {
            //添加维修
            start(AddRepairActivity::class.java)
        }

        @JavascriptInterface
        fun androidaddnotice(id: String?) {
            //公告管理
            start(NoticeActivity::class.java)
        }

        @JavascriptInterface
        fun androidculture(id: String?) {
            //文化管理
            Log.i("TagA", "文化管理")
        }

        @JavascriptInterface
        fun androidnotice(id: String?) {
            //公告管理
            Log.i("TagA", "公告")
        }

        @JavascriptInterface
        fun androidattendance(id: String?) {
            //考勤管理
            start(SignMonthActivity::class.java)
        }

        @JavascriptInterface
        fun androidproperty(id: String?) {
            //物业审核
            Log.i("TagA", "物业审核")
        }

        @JavascriptInterface
        fun androidcheckculture(id: String?) {
            //文化审核
            Log.i("TagA", "文化审核")
        }

        @JavascriptInterface
        fun androidpeople(id: String?) {
            //人资管理
            Log.i("TagA", "人资管理")
        }
    }

    private fun start(clazz: Class<*>) {
        val intent = Intent(this, clazz)
        startActivity(intent)
    }
}